<?php
//display quote 
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/salesOrderConvertUtil.js', CClientScript::POS_BEGIN);

$countQuote = count($quoteList);

$table = new CI_Table();
$tmpl = array(
    'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
);
$table->set_template($tmpl);
$table->set_heading(array(
    'Date', 'Reference', 'Quote No', 'Status', 'Amount'
));
echo "<h2>QUOTES</h2>";
$quote_total = 0;
if ($countQuote > 0) {
    for ($i = 0; $i < $countQuote; $i++) {

        $item = $quoteList[$i];
        /* @var $item Invoice */

        $data = array(
            $item->invoicedate,
            NULL,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            $item->getQuoteStatusText(),
            '<div style="text-align: right;">' . number_format($item->getAmountWithDiscount(), 2) . '<div>',

        );
        $quote_total += $item->getAmountWithDiscount();
        $table->add_row($data);
    }
    $table->add_row(array('<br/>', NULL, NULL, NULL, NULL));
    $data = array(
        'Total',
        NULL,
        NULL,
        NULL,
        '<div style="text-align: right;" >' . number_format($quote_total, 2) . '<div>',

    );
    $table->add_row($data);
    echo $table->generate();
} else {
    echo "No quote have been created with this job number.";
}
?>

<h2>SALES ORDERS</h2>
<?php
if (isset($jobModel))
    $refacc = $jobModel->customer->quickbook . ':' . $jobModel->jobid;
$soCount = count($salesOrderList);


$table->set_heading(array(
    'Date', 'Reference', 'Sales Order No', 'Status', 'Amount', 'Deposits',
));
$table->set_template(array(
    'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
));
if ($soCount > 0) {
    $convertUtil = new SalesOrderPaymentConvertUtil();
    if (isset($jobModel)) {
        $criteria = new CDbCriteria();
        $criteria->condition = "customerRefFullName='$refacc'";
        $criteria->order = "date Asc";
        $paymentsApplied = QbPaymentsapplied::model()->findAll($criteria);
        $paymentList = $paymentsApplied;
        $criteria = new CDbCriteria();
        $criteria->condition = "customerRefFullName='$refacc' and type = 'credit' and account='DOWNPAYMENT' ";
        $criteria->order = "date Asc";
        $depositsList = QuickbooksJournals::model()->findAll($criteria);

    } else {
        $paymentList = array();
        $depositsList = array();
    }
    $paymentStr = '';
    foreach ($salesOrderList as $item) {

        $depositsStr = '';
        if (count($depositsList) > 0) {

            foreach ($depositsList as $payment) {
                $no = str_replace('2012-', '', $item->invoice);

                //generate the table
                $payTable = new CI_Table;
                $payTable->set_template(array(
                    'table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="" width="200">',
                ));
                $payTable->set_heading(array(
                    'Amount', 'Memo'
                ));
                //can apply to invoice, must have invoice to apply

                $payTable->add_row(array($payment->totalAmount, $payment->memo));
                //register the tooltip script

                // if(strpos($payment->memo, $no)){
                if (true) {
                    Yii::app()->clientScript->registerScript("payment-$payment->refNumber", "
                                        $('.payment-$payment->refNumber').hide();
                                        $('#payment-$payment->refNumber').qtip({
                                            content:$('.payment-$payment->refNumber')
                                            });
                                        ");


                    $depositsStr .= <<<HERE
                                        <a href="#" id='payment-$payment->refNumber'>$payment->totalAmount - $payment->memo</a> 
                                        <div  class='payment-$payment->refNumber'>
                                            {$payTable->generate()}
                                        </div><br/>
                                        
                                        
HERE;
                }
            }
        }
        $data = array(
            $item->invoicedate,
            $item->quoteorg,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            JobsStatus::getText($item->salesorderstatus),
            number_format($item->getAmountWithDiscount(), 2),

            $depositsStr
        );
        $table->add_row($data);
    }
    echo $table->generate();
} else {
    echo 'No sales orders have been created with this job number.';
}
?>
<br/>
<h2>INVOICES & CREDIT MEMOS</h2>
<?php
$invCount = count($invList);
$table->set_template(array(
    'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
));
$table->set_heading(array(
    'Type', 'Date', 'Reference', 'Number', 'Status', 'Amount', 'Payments'
));
if ($invCount > 0) {

    $inv_total = 0;
    $inv_payments_total = 0;
    foreach ($invList as $item) {
        /**@var $item Invoice */
        $criteria = new CDbCriteria();
        $criteria->condition = "invoiceNo='$item->invoice'";
        $criteria->order = "date Asc";
        $paymentsApplied = QbPaymentsapplied::model()->findAll($criteria);
        $paymentList = $paymentsApplied;
        $paymentStr = '';


        if ($item->invoicetype == Invoice::TYPE_CREDIT_MEMO) {
            $reference = $item->invoiceorg;
            $itemStatus = '';
        } else {
            $reference = $item->salesordersorg;
            $itemStatus = $item->getQuoteStatusText();
        }
        $data = array(
            $item->invoicetype,
            $item->invoicedate,
            $reference,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            $itemStatus,
            '<div style="text-align: right;" >' . number_format($item->getAmountWithDiscount(), 2) . '</div>',
            '<div style="text-align: right;" >' . number_format($item->getPaymentApplied(), 2) . '</div>',
        );
        $table->add_row($data);
        $inv_total = $item->getAmountWithDiscount();
        $inv_payments_total = $item->getPaymentApplied();
    }
    $table->add_row(array('<br/>', NULL, NULL, NULL, NULL, NULL, NULL));
    $data = array(
        'Total',
        NULL,
        NULL,
        NULL,
        NULL,
        '<div style="text-align: right;" >' . number_format($inv_total, 2) . '<div>',
        '<div style="text-align: right;" >' . number_format($inv_payments_total, 2) . '<div>',

    );
    $table->add_row($data);
    echo $table->generate();
} else {
    echo 'No invoice have been created with this job number.';
}
?>
